package com.ecoolex.chinadutyfree.retailer.admin.graphql;

import java.util.Date;
import java.util.List;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.chinadutyfree.retailer.admin.core.AdminConstant;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderIncrementData;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderTotalData;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsProductIncrementData;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsProductTotalData;
import com.ecoolex.chinadutyfree.retailer.entity.UserOrderData;
import com.ecoolex.chinadutyfree.retailer.pojo.OrderStatisticsIncrementDTO;
import com.ecoolex.chinadutyfree.retailer.ws.DataAnalyseService;
import com.ecoolex.framework.common.annotation.Description;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberManager;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;

@GraphQL2Module(type=Type.Admin,description = "数据统计分析")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY,key=DataAnalyseGraphql.AUTH,name="数据统计分析")
public class DataAnalyseGraphql {
	
	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-DATA-ANALYSE";
	private static final String AUTH_ORDER_TOTAL = AUTH + "-ORDER-TOTAL";
	private static final String AUTH_ORDER_TOTAL_REFRESH = AUTH + "-ORDER-TOTAL-REFRESH";
	private static final String AUTH_ORDER_INCREMENT_REFRESH = AUTH + "-ORDER-INCREMENT-REFRESH";
	private static final String AUTH_ORDER_INCREMENT = AUTH + "-ORDER-INCREMENT";
	private static final String AUTH_ORDER_INCREMENT_LIST = AUTH + "-ORDER-INCREMENT-LIST";
	private static final String AUTH_PRODUCT_TOTAL = AUTH + "-PRODUCT-TOTAL";
	private static final String AUTH_PRODUCT_INCREMENT = AUTH + "-PRODUCT-INCREMENT";
	private static final String AUTH_USER_ORDER = AUTH + "-USER-ORDER";
	
	@Reference
	private DataAnalyseService dataAnalyse;
	
	@Autowired
	private ConsumerMemberManager consumerMemberManager;
	
	@TypeField(type = UserOrderData.class, field = "userName", description = "用户名称")
	public ConsumerMember userName(@Source UserOrderData source) {
		return consumerMemberManager.getByUserId(source.getUserId());
	}
	
	@TypeField(type = UserOrderData.class, field = "createTime", description = "注册时间")
	public ConsumerMember createTime(@Source UserOrderData source) {
		return consumerMemberManager.getByUserId(source.getUserId());
	} 
	
	@QueryField(field="orderTotalData",description = "查询订单总量数据")
	@AdminAuthoritativeRegister(parentKey = AUTH,key=AUTH_ORDER_TOTAL,name="查询订单总量数据")
	public DirectMailGoodsOrderTotalData getLatestOrderTotalData(AdminMemberAuthority user) {
		user.assertAuth(AUTH_ORDER_TOTAL);
		return dataAnalyse.getLatestOrderTotalData();
	}
	
	@QueryField(field="orderIncrementData",description = "根据日期查询订单增量总和")
	@AdminAuthoritativeRegister(parentKey = AUTH,key=AUTH_ORDER_INCREMENT,name="根据日期查询订单增量总和")
	public OrderStatisticsIncrementDTO getOrderIncrementData(Date startDate,Date endDate,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ORDER_INCREMENT);
		return dataAnalyse.getOrderIncrementDataByDate(startDate, endDate);
	}
	
	@QueryField(field="orderIncrementListData",description = "根据日期查询订单增量列表")
	@AdminAuthoritativeRegister(parentKey = AUTH,key=AUTH_ORDER_INCREMENT_LIST,name="根据日期查询订单增量列表")
	public List<DirectMailGoodsOrderIncrementData> getOrderIncrementListData(Date startDate,Date endDate,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ORDER_INCREMENT_LIST);
		return dataAnalyse.getIncrementOrderListByDate(startDate, endDate);
	}
	
	@QueryField(field="productTotalData",description = "查询商品总量数据")
	@AdminAuthoritativeRegister(parentKey = AUTH,key=AUTH_PRODUCT_TOTAL,name="查询商品总量数据")
	public DirectMailGoodsProductTotalData getLatestProductTotalData(AdminMemberAuthority user) {
		user.assertAuth(AUTH_PRODUCT_TOTAL);
		return dataAnalyse.getLatestProductTotalData();
	}
	
	@QueryField(field="productIncrementData",description = "查询商品增量数据列表")
	@AdminAuthoritativeRegister(parentKey = AUTH,key=AUTH_PRODUCT_INCREMENT,name="查询商品增量数据列表")
	public Pagination<DirectMailGoodsProductIncrementData> queryProductIncrementData(Date startDate,Date endDate,
		String customName,Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user) {
		user.assertAuth(AUTH_PRODUCT_INCREMENT);
		return dataAnalyse.queryProductIncrement(startDate, endDate, customName, sorts, page);
	}
	
	@QueryField(field="userOrderData",description = "查询用户订单增量数据列表")
	@AdminAuthoritativeRegister(parentKey = AUTH,key=AUTH_USER_ORDER,name="查询用户订单增量数据列表")
	public Pagination<UserOrderData> queryUserOrderData(Date startDate,Date endDate,
		Integer userId,Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user) {
		user.assertAuth(AUTH_USER_ORDER);
		
		return dataAnalyse.queryUserOrder(startDate, endDate, userId, sorts, page);
	}
	
	@QueryField(field="refreshOrderTotalData",description = "刷新订单总量数据")
	@AdminAuthoritativeRegister(parentKey = AUTH,key=AUTH_ORDER_TOTAL_REFRESH,name="刷新订单总量数据")
	public Boolean refreshLatestOrderTotalData(AdminMemberAuthority user) {
		user.assertAuth(AUTH_ORDER_TOTAL_REFRESH);
		return dataAnalyse.refreshOrderTotalData(user.userName());
	}
	
	@QueryField(field="refreshOrderIncrementData",description = "订单增量数据修复专用")
	@AdminAuthoritativeRegister(parentKey = AUTH,key=AUTH_ORDER_INCREMENT_REFRESH,name="订单增量数据修复")
	public Boolean refreshOrderIncrementData(@NonNull @Description("目标修复时间") Date statisticsDate,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ORDER_INCREMENT_REFRESH);
		return dataAnalyse.refreshOrderIncrementData(user.userName(), statisticsDate);
	}
}
